<template>
  <e-table
    ref="eTable"
    @getList="handleGetList"
    :tableData="tableData"
    :columns="columns"
    :loading="loading"
    :total="total"
  >
    <el-table-column slot="action" fixed="right" width="50" align="center">
      <template slot-scope="scope">
        <el-dropdown>
          <a class="el-dropdown-link">
            <i class="el-icon-menu"></i>
          </a>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>
              <a @click="handleEdit( true,scope.row)">编辑</a>
            </el-dropdown-item>
            <el-dropdown-item>
              <a class="red" @click="handleDelete(scope.row)">删除</a>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </el-table-column>
  </e-table>
</template>

<script>
import { cyclePage, cycleDelete } from '@/api/morality'

import { openNotification } from '@/utils/util'
export default {
  data() {
    return {
      tableData: [],
      loading: false,
      total: 1,
      queryParam: {
        pageSize: 10,
        pageNo: 1
      },
      columns: [
        {
          label: '周期名称',
          prop: 'moralityCycleName'
        },
        {
          label: '期间开始时间',
          prop: 'cycleStartDate'
        },
        {
          label: '期间结束时间',
          prop: 'cycleEndDate'
        },

        { slot: 'action' }
      ]
    }
  },
  mounted() {
    this.handleGetList()
  },
  methods: {
    handleEdit(bool, obj) {
      this.$emit('edit', bool, obj)
    },
    async handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      const res = await cyclePage(this.queryParam)

      if (res && res.data.records.length > 0) {
        const {
          state,
          data: { records, total }
        } = res
        this.tableData = records
        this.total = total
      } else {
        this.tableData = []
        this.total = 1
      }
      this.loading = false
    },
    handleDelete(row) {
      console.log('row', row)
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.moralityCycleName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await cycleDelete(row.moralityCycleId, {})
          openNotification('success', '删除成功')
          const index = _this.tableData.findIndex(v => v.moralityCycleId === row.moralityCycleId)
          _this.tableData.splice(index, 1)
        }
      })
    }
  }
}
</script>